package com.ruoyi.supermarket.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.supermarket.bean.NewProductRes;
import com.ruoyi.supermarket.domain.Product;
import com.ruoyi.supermarket.service.ProductService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.supermarket.domain.NewProduct;
import com.ruoyi.supermarket.service.NewProductService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 新鲜好物Controller
 * 
 * @author 李其伟
 * @date 2023/10/24
 */
@RestController
@RequestMapping("/supermarket/newProduct")
public class NewProductController extends BaseController
{
    @Autowired
    private NewProductService newProductService;

    @Autowired
    private ProductService productService;
    /**
     * 查询新鲜好物列表
     */
    @PreAuthorize("@ss.hasPermi('supermarket:newProduct:list')")
    @GetMapping("/list")
    public TableDataInfo list(NewProduct newProduct)
    {
        startPage();
        List<NewProduct> list = newProductService.selectNewProductList(newProduct);
        TableDataInfo tableDataInfo =  getDataTable(list);
        List<NewProduct> newProductList = (List<NewProduct>) tableDataInfo.getRows();

        if(newProductList.size()==0){
            return tableDataInfo;
        }
        String[] skus = newProductList.stream().map(NewProduct::getSku).toArray(String[]::new);
        List<Product> productList = productService.selectProductBySkus(skus);
        Map<String, Product> productMap = productList.stream().collect(Collectors.toMap(Product::getSku, product->product));

        List<NewProductRes> newProductResList = new ArrayList<>();
        for(NewProduct np : newProductList){
            NewProductRes res = new NewProductRes();
            res.setNewProduct(np);
            res.setProduct(productMap.get(np.getSku()));
            newProductResList.add(res);
        }
        tableDataInfo.setRows(newProductResList);
        return tableDataInfo;
    }

    /**
     * 导出新鲜好物列表
     */
    @PreAuthorize("@ss.hasPermi('supermarket:newProduct:export')")
    @Log(title = "新鲜好物", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, NewProduct newProduct)
    {
        List<NewProduct> list = newProductService.selectNewProductList(newProduct);
        ExcelUtil<NewProduct> util = new ExcelUtil<NewProduct>(NewProduct.class);
        util.exportExcel(response, list, "新鲜好物数据");
    }

    /**
     * 获取新鲜好物详细信息
     */
    @PreAuthorize("@ss.hasPermi('supermarket:newProduct:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(newProductService.selectNewProductById(id));
    }

    /**
     * 新增新鲜好物
     */
    @PreAuthorize("@ss.hasPermi('supermarket:newProduct:add')")
    @Log(title = "新鲜好物", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody NewProduct newProduct)
    {
        return toAjax(newProductService.insertNewProduct(newProduct));
    }

    /**
     * 修改新鲜好物
     */
    @PreAuthorize("@ss.hasPermi('supermarket:newProduct:edit')")
    @Log(title = "新鲜好物", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody NewProduct newProduct)
    {
        return toAjax(newProductService.updateNewProduct(newProduct));
    }

    /**
     * 上下架新鲜好物
     */
    @PreAuthorize("@ss.hasPermi('supermarket:newProduct:publish')")
    @Log(title = "新鲜好物", businessType = BusinessType.GRANT)
    @PostMapping("/publish")
    public AjaxResult publish(@RequestBody NewProduct newProduct)
    {
        return toAjax(newProductService.publishNewProduct(newProduct));
    }

    /**
     * 删除新鲜好物
     */
    @PreAuthorize("@ss.hasPermi('supermarket:newProduct:remove')")
    @Log(title = "新鲜好物", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(newProductService.deleteNewProductByIds(ids));
    }
}
